<script>

function editSerRecord(id) {
    $("#service_record_form")[0].reset();
    $('#serviceRecordModal').modal();
    
    cAjax({
                    url:  "/crm/serviceRecord/editSerRecord",
                    data: {id:id},
                    success: function(data){
                                if(data.result) {
                                     $("#service_record_id").val(data.data.id);     
                                        $("#service_record_uid").val(data.data.uid);
                                        $("#contactType").val(data.data.contactType);
                                        $("#serviceWay").val(data.data.serviceWay);
                                        $("#serviceAim").val(data.data.serviceAim);
                                        $("#serviceContent").val(data.data.serviceContent);                            
                                } else {
                                    greeting({msg:"<h4 style=\"color:red\">失败，有错误！</h4>",title:"操作失败"});
                                }
                     },
                    error: function(obj) {
                    }
        }); 
            
}

function validateSerRecord(contactType,serviceWay,serviceAim) {
    $("#service_record_form .form-group").removeClass("has-error");
    var flag = true;
    if(contactType=="" ) {
        $("#contactType").parents("div.form-group").addClass("has-error");
        flag = false;
    }
    if(serviceWay=="") {
        $("#serviceWay").parents("div.form-group").addClass("has-error");
        flag = false;
    }
    if(serviceAim=="") {
        $("#serviceAim").parents("div.form-group").addClass("has-error");
        flag = false;
    }
    
    return flag;
}

$(function(){

    $("#add_service_record").click(function(){
        $("#service_record_form")[0].reset();
         $('#serviceRecordModal').modal();
    });
    
    $("#save_service_record").click(function(){
        var id = $("#service_record_id").val();
        var uid = $("#service_record_uid").val();
        var contactType = $("#contactType").val();
        var serviceWay = $("#serviceWay").val();
        var serviceAim = $("#serviceAim").val();
        var serviceContent = $("#serviceContent").val();
        
        if(!validateSerRecord(contactType,serviceWay,serviceAim)) {
            return false;
        }
        
        cAjax({
                    url:  "/crm/serviceRecord/saveSerRecord",
                    data: {id:id,uid:uid,contactType:contactType,serviceWay:serviceWay,serviceContent:serviceContent,serviceAim:serviceAim},
                    success: function(data){
                                if(data.result) {
                                     $('#serviceRecordModal').modal('hide');
                                     $("#service_record_form")[0].reset();
                                    greeting({msg:"操作成功！", confirm:function(){
                                        location.reload();
                                    }});                                
                                } else {
                                    greeting({msg:"<h4 style=\"color:red\">失败，有错误！</h4>",title:"操作失败"});
                                }
                     },
                    error: function(obj) {
                    }
        }); 
            
    });
    
});
</script>